<?php

namespace App\Service\Push;

use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Enums\OpenShop\InvestmentPromotion\SiteMissionEnum;
use App\Enums\OpenShop\MissionTemplateEnum;
use App\Enums\WorkOrder\MissionEnum;
use App\Enums\WorkOrder\OrderEnum;
use App\Models\College\CollegeTrainingOrderModel;
use App\Models\Common\ConfigModel;
use App\Models\Common\FsCompanyRoleMappingModel;
use App\Models\Cruise\CruiseCheckDetailModel;
use App\Models\Cruise\CruiseCheckTaskModel;
use App\Models\Cruise\CruiseOrderModel;
use App\Models\Cruise\CruiseOrderProcessModel;
use App\Models\Cruise\CruiseTemplateModel;
use App\Models\OpenShop\Common\OpenShopOrderManagerModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopOrganizationRegionMappingModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSiteMissionModel;
use App\Models\OpenShop\SiteLocation\OpenShopSiteLocationExamineLogModel;
use App\Models\OpenShop\SiteLocation\OpenShopSiteLocationModel;
use App\Models\WorkOrder\WorkOrderApplyMainModel;
use App\Models\WorkOrder\WorkOrderUserRoleModel;
use App\Remote\Rms\RmsUserRemote;
use App\Service\BaseService;
use App\Service\Cruise\Mission\CheckInputService;
use App\Service\OpenShop\Feishu\OrganizationManageService;
use App\Service\OpenShop\FeishuMini\SiteLocationFeiShuMiniService;
use App\Service\OpenShop\SiteLocation\SiteLocationService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Str;

/**
 * 推送，基础任务的sql获取
 */
class PushBaseFuncService extends PushAbstractFuncService
{
    /**
     * 商家加盟费待支付
     * @return array
     */
    public function task101(): array
    {
        return $this->getSignOrderData([SignOrderEnum::STEP_3, SignOrderEnum::STEP_2]);
    }

    /**
     * 商家加盟费待签约
     * @return array
     */
    public function task102(): array
    {
        return $this->getSignOrderData([SignOrderEnum::STEP_4]);
    }

    /**
     * 商家加盟数据
     * @param $steps
     * @return array
     */
    public function getSignOrderData($steps): array
    {
        return OpenShopSignOrderModel::query()
            ->whereIn('step', $steps)
            ->where('status', SignOrderEnum::ORDER_STATUS_OK)
            ->get()
            ->map(function (OpenShopSignOrderModel $openShopSignOrderModel) {
                $userId    = $openShopSignOrderModel->investmentManagerId;
                $createdAt = $openShopSignOrderModel->createdAt->timestamp;
                $uuid      = $openShopSignOrderModel->id;
                return $this->taskResponse($userId, $createdAt, $uuid);
            })->toArray();
    }

    /**
     * 设计师待指派
     * @return array
     */
    public function task111(): array
    {
        return $this->getSiteMissionData(MissionTemplateEnum::STEP_STATUS_7);
    }

    /**
     * 下设计单
     * @return array
     */
    public function task112(): array
    {
        return $this->getSiteMissionData(MissionTemplateEnum::STEP_STATUS_13);
    }

    /**
     * 下设备单
     * @return array
     */
    public function task113(): array
    {
        return $this->getSiteMissionData(MissionTemplateEnum::STEP_STATUS_15);
    }

    /**
     * 下软装单
     * @return array
     */
    public function task114(): array
    {
        return $this->getSiteMissionData(MissionTemplateEnum::STEP_STATUS_8);
    }

    /**
     * 确认建店
     * @return array
     */
    public function task121(): array
    {
        return $this->getSiteMissionData(MissionTemplateEnum::STEP_STATUS_6);
    }

    /**
     * 商家设计款支付
     * @return array
     */
    public function task122(): array
    {
        return $this->getSiteMissionData(MissionTemplateEnum::STEP_STATUS_14, 1);
    }

    /**
     * 商家设备款支付
     * @return array
     */
    public function task123(): array
    {
        return $this->getSiteMissionData(MissionTemplateEnum::STEP_STATUS_16, 1);
    }

    /**
     * 商家软装款支付
     * @return array
     */
    public function task124(): array
    {
        return $this->getSiteMissionData(MissionTemplateEnum::STEP_STATUS_17, 1);
    }

    /**
     * 进场待确认
     * @return array
     */
    public function task125(): array
    {
        return $this->getSiteMissionData(MissionTemplateEnum::STEP_STATUS_9);
    }

    /**
     * 进场待施工
     * @return array
     */
    public function task126(): array
    {
        return $this->getSiteMissionData(MissionTemplateEnum::STEP_STATUS_10);
    }

    /**
     * 新店帮扶
     * @return array
     */
    public function task127(): array
    {
        $list = [];

        CruiseOrderModel::query()
                        ->where('status', CruiseOrderModel::STATUS_NOT_REVIEW)
                        ->where('type', CruiseOrderModel::TYPE_NEW_SHOP_HELP)
                        ->get()
                        ->each(function (CruiseOrderModel $model) use (&$list) {
                            $createdAt = $model->createdAt->timestamp;
                            $uuid      = $model->id;
                            $list[]    = $this->taskResponse($model->handlerId, $createdAt, $uuid);
                        });
        return $list;
    }

    /**
     * 开发待指派
     * @return array
     */
    public function task131(): array
    {
        return $this->getSiteMissionData(MissionTemplateEnum::STEP_STATUS_3);
    }

    /**
     * 选址待审核 (总部负责人审核)
     * @return array
     */
    public function task132(): array
    {
        //获取总部负责人
        $siteSuperManager = ConfigModel::getManager(ConfigModel::SITE_SUPER_ADMIN);
        $list             = [];
        OpenShopSiteLocationModel::query()
            ->where('source', OpenShopSiteLocationModel::SOURCE_OPEN_SHOP)
            ->where('siteStatus', OpenShopSiteLocationModel::SITE_STATUS_11)
            ->with(['signOrderRel'])
            ->get()
            ->each(function (OpenShopSiteLocationModel $openShopSiteLocationModel) use (&$list, $siteSuperManager) {
                if (null != $openShopSiteLocationModel->signOrderRel && in_array($openShopSiteLocationModel->signOrderRel->status, [SignOrderEnum::ORDER_STATUS_OK, SignOrderEnum::ORDER_STATUS_COMPLETE])) {
                    $createdAt = $openShopSiteLocationModel->createdAt->timestamp;
                    $uuid      = $openShopSiteLocationModel->id;
                    foreach ($siteSuperManager as $manager) {
                        $list[] = $this->taskResponse($manager, $createdAt, $uuid);
                    }
                }
            });
        return $list;
    }

    /**
     * 选址待提交
     * @return array
     */
    public function task141(): array
    {
        $list = [];
        OpenShopSiteLocationModel::query()
            ->where('source', OpenShopSiteLocationModel::SOURCE_OPEN_SHOP)
            ->whereIn('siteStatus', [OpenShopSiteLocationModel::SITE_STATUS_0, OpenShopSiteLocationModel::SITE_STATUS_1])
            ->with(['signOrderRel'])
            ->get()
            ->each(function (OpenShopSiteLocationModel $openShopSiteLocationModel) use (&$list) {
                if (null != $openShopSiteLocationModel->signOrderRel && in_array($openShopSiteLocationModel->signOrderRel->status, [SignOrderEnum::ORDER_STATUS_OK, SignOrderEnum::ORDER_STATUS_COMPLETE])) {
                    $userId    = $openShopSiteLocationModel->developConsultantId;
                    $createdAt = $openShopSiteLocationModel->createdAt->timestamp;
                    $uuid      = $openShopSiteLocationModel->id;
                    $list[]    = $this->taskResponse($userId, $createdAt, $uuid);
                }
            });
        return $list;
    }

    /**
     * 选址待修改
     * @return array
     */
    public function task142(): array
    {
        $list = [];
        OpenShopSiteLocationModel::query()
            ->where('source', OpenShopSiteLocationModel::SOURCE_OPEN_SHOP)
            ->where('siteStatus', OpenShopSiteLocationModel::SITE_STATUS_3)
            ->with(['signOrderRel'])
            ->get()
            ->each(function (OpenShopSiteLocationModel $openShopSiteLocationModel) use (&$list) {
                if (null != $openShopSiteLocationModel->signOrderRel && in_array($openShopSiteLocationModel->signOrderRel->status, [SignOrderEnum::ORDER_STATUS_OK, SignOrderEnum::ORDER_STATUS_COMPLETE])) {
                    $userId    = $openShopSiteLocationModel->developConsultantId;
                    $createdAt = $openShopSiteLocationModel->createdAt->timestamp;
                    $uuid      = $openShopSiteLocationModel->id;
                    $list[]    = $this->taskResponse($userId, $createdAt, $uuid);
                }
            });
        return $list;
    }

    /**
     * 房源待修改
     * @return array
     */
    public function task143(): array
    {
        $list = [];
        OpenShopSiteLocationModel::query()
            ->where('source', OpenShopSiteLocationModel::SOURCE_HOUSING)
            ->where('clueStatus', OpenShopSiteLocationModel::CLUE_STATUS_4)
            ->with(['signOrderRel'])
            ->get()
            ->each(function (OpenShopSiteLocationModel $openShopSiteLocationModel) use (&$list) {
                $userId    = $openShopSiteLocationModel->developConsultantId;
                $createdAt = $openShopSiteLocationModel->createdAt->timestamp;
                $uuid      = $openShopSiteLocationModel->id;
                $list[]    = $this->taskResponse($userId, $createdAt, $uuid);
            });
        return $list;
    }

    /**
     * 商家待确认
     * @return array
     */
    public function task144(): array
    {
        return $this->getSiteMissionData(MissionTemplateEnum::STEP_STATUS_5);
    }

    /**
     * 获得开店数据
     * @param $stepStatus
     * @param int $type  区分类型  特殊类型type为1 （从门店管理表里面拿数据）
     * @return array
     */
    public function getSiteMissionData($stepStatus, int $type = 0): array
    {
        return OpenShopSiteMissionModel::query()
            ->where('status', SiteMissionEnum::STATUS_1)
            ->where('orderStatus', SignOrderEnum::ORDER_STATUS_OK) //和 sign_order 表的 status 同步 [0 关闭 1 正常]
            ->where('stepStatus', $stepStatus)
            ->with(['getOrderManagerRel'])
            ->get()
            ->map(function (OpenShopSiteMissionModel $openShopSiteMissionModel) use ($type){
                $userId = 0;
                if ($type == 1) {
                    foreach ($openShopSiteMissionModel->getOrderManagerRel as $orderManager) {
                        if ($orderManager->type == OpenShopOrderManagerModel::MANAGER_TYPE_4) {
                            $userId = $orderManager->managerUid;
                        }
                    }
                } else {
                    $userId    = $openShopSiteMissionModel->dealUid;
                }
                $createdAt = $openShopSiteMissionModel->createdAt->timestamp;
                $uuid      = $openShopSiteMissionModel->id;
                return $this->taskResponse($userId, $createdAt, $uuid);
            })->toArray();
    }


    /**
     * 房源待审核
     * @return array
     * @throws \Exception
     */
    public function task145(): array
    {
        $list = [];
        OpenShopSiteLocationModel::query()
            ->where('source', OpenShopSiteLocationModel::SOURCE_HOUSING)
            ->where('clueStatus', OpenShopSiteLocationModel::CLUE_STATUS_11)
            ->with(['examineLogsRel'])
            ->get()
            ->each(function (OpenShopSiteLocationModel $openShopSiteLocationModel) use (&$list) {
                // 获取省区处理人
                $provinceManagerId = OrganizationManageService::getProvinceInfoByProvinceCode($openShopSiteLocationModel->provinceCode)['operationUid'] ?? 0;
                $createdAt         = $openShopSiteLocationModel->createdAt->timestamp;
                $uuid              = $openShopSiteLocationModel->id;

                $keyPoint = SiteLocationFeiShuMiniService::getKeyPointByOrderNo($openShopSiteLocationModel->orderNo);

                //判断是否为招商
                if (SiteLocationService::isSolicitBusinessManager($openShopSiteLocationModel->createdByUid)) {
                    $approveProcedure     = OpenShopSiteLocationExamineLogModel::APPROVE_PROCEDURE["solicitBusinessManagerApprove"];
                    $nextApproveRoleIndex = OpenShopSiteLocationExamineLogModel::getExistsApproveCount($openShopSiteLocationModel->orderNo);
                    $handlerArr           = OpenShopSiteLocationExamineLogModel::getApproveProcedureUser(
                        $approveProcedure[$nextApproveRoleIndex],
                        $openShopSiteLocationModel
                    );
                    foreach ($handlerArr as $handler) {
                        $list[] = $this->taskResponse($handler, $createdAt, $uuid);
                    }
                } else {
                    if ($keyPoint) {
                        $list[] = $this->taskResponse($provinceManagerId, $createdAt, $uuid);
                    } else {
                        $handlerArr = explode(',', $openShopSiteLocationModel->handler);
                        foreach ($handlerArr as $handler) {
                            $list[] = $this->taskResponse($handler, $createdAt, $uuid);
                        }
                    }
                }
            });
        return $list;
    }


    /**
     * 选址待审批
     * @return array
     */
    public function task146(): array
    {
        $list = [];
        OpenShopSiteLocationModel::query()
            ->where('source', OpenShopSiteLocationModel::SOURCE_OPEN_SHOP)
            ->where('siteStatus', OpenShopSiteLocationModel::SITE_STATUS_2)
            ->with(['examineLogsRel'])
            ->get()
            ->each(function (OpenShopSiteLocationModel $openShopSiteLocationModel) use (&$list) {
                $handlerArr = explode(',', $openShopSiteLocationModel->handler);
                foreach ($handlerArr as $handler) {
                    $list[] = $this->taskResponse($handler, $openShopSiteLocationModel->createdAt->timestamp, $openShopSiteLocationModel->id);
                }
            });
        return $list;
    }


    /**
     * 增长-待提交
     * @return array
     * @throws GuzzleException
     */
    public function task151(): array
    {
        return $this->getIncreaseData(CruiseTemplateModel::TYPE_1, CruiseOrderModel::STATUS_NOT_SUBMIT);
    }


    /**
     * 增长-待修改
     * @return array
     * @throws GuzzleException
     */
    public function task152(): array
    {
        return $this->getIncreaseData(CruiseTemplateModel::TYPE_1, CruiseOrderModel::STATUS_NOT_MODIFY);
    }


    /**
     * 增长-待审批
     * @return array
     * @throws GuzzleException
     */
    public function task153(): array
    {
        return $this->getIncreaseData(CruiseTemplateModel::TYPE_1, CruiseOrderModel::STATUS_NOT_REVIEW);
    }


    /**
     * 获得增长数据
     * @return array
     * @throws GuzzleException
     */
    public function getIncreaseData($type, $status): array
    {
        $list = [];
        CruiseOrderModel::query()
            ->where('type', $type)
            ->where('status', $status)
            ->with(['cruiseOrderProcessRel'])
            ->get()
            ->each(function (CruiseOrderModel $cruiseOrderModel) use (&$list) {
                $step       = 0;
                $stepStatus = 0;
                foreach ($cruiseOrderModel->cruiseOrderProcessRel as $cruiseOrderProcess) {
                    $step       = $cruiseOrderProcess->step;
                    $stepStatus = $cruiseOrderProcess->status;
                }
                //获得处理人
                $handlerIds = $this->getCruiseProcess($step, $stepStatus, $cruiseOrderModel);
                foreach ($handlerIds as $handlerId) {
                    $createdAt = $cruiseOrderModel->createdAt->timestamp;
                    $uuid      = $cruiseOrderModel->id;
                    $list[]    = $this->taskResponse($handlerId, $createdAt, $uuid);
                }
            });
        return $list;
    }


    /**
     * 稽核-待提交
     * @return array
     * @throws GuzzleException
     */
    public function task161(): array
    {
        return $this->getTaskData(CruiseTemplateModel::TYPE_3, "id in (select `orderId` from `cruise_check_details` where `checkType` in (0, 1))", CruiseOrderModel::STATUS_NOT_SUBMIT);
    }


    /**
     * 三方稽核-待提交
     * @return array
     * @throws GuzzleException
     */
    public function task162(): array
    {
        return $this->getTaskData(CruiseTemplateModel::TYPE_3, "id in (select `orderId` from `cruise_check_details` where `checkType` = 2)", CruiseOrderModel::STATUS_NOT_SUBMIT);
    }


    /**
     * 三方稽核-待修改
     * @return array
     * @throws GuzzleException
     */
    public function task163(): array
    {
        return $this->getTaskData(CruiseTemplateModel::TYPE_3, "id in (select `orderId` from `cruise_check_details` where `checkType` = 2)", CruiseOrderModel::STATUS_NOT_MODIFY);
    }


    /**
     * 稽核-待审核
     * @return array
     * @throws GuzzleException
     */
    public function task164(): array
    {
        return $this->getTaskData(CruiseTemplateModel::TYPE_3, "id in (select `orderId` from `cruise_check_details` where `checkType` in (0, 1))", CruiseOrderModel::STATUS_NOT_REVIEW);
    }


    /**
     * 获取稽核数据
     * @param $type
     * @param $raw
     * @param $status
     * @return array
     * @throws GuzzleException
     */
    public function getTaskData($type, $raw, $status): array
    {
        $list = [];
        CruiseOrderModel::query()
            ->where('type', $type)
            ->whereRaw($raw)
            ->where('status', $status)
            ->with(['cruiseOrderProcessRel', 'cruiseCheckTaskRel'])
            ->get()
            ->each(function (CruiseOrderModel $cruiseOrderModel) use (&$list) {
                if ((int)optional($cruiseOrderModel->cruiseCheckTaskRel)->status == CruiseCheckTaskModel::STATUS_ING) {
                    $step       = 0;
                    $stepStatus = 0;
                    foreach ($cruiseOrderModel->cruiseOrderProcessRel as $cruiseOrderProcess) {
                        $step       = $cruiseOrderProcess->step;
                        $stepStatus = $cruiseOrderProcess->status;
                    }
                    //获得处理人
                    $handlerIds = $this->getCruiseProcess($step, $stepStatus, $cruiseOrderModel);
                    foreach ($handlerIds as $handlerId) {
                        $createdAt = $cruiseOrderModel->createdAt->timestamp;
                        $uuid      = $cruiseOrderModel->id;
                        $list[]    = $this->taskResponse($handlerId, $createdAt, $uuid);
                    }
                }
            });
        return $list;
    }


    /**
     * 创业商学院 - 商学院待确认
     * @return array
     */
    public function task171(): array
    {
        return $this->getCollegeData(CollegeTrainingOrderModel::STATUS_COLLEGE_CONFIRM);
    }


    /**
     * 创业商学院 - 商家待确认
     * @return array
     */
    public function task172(): array
    {
        return $this->getCollegeData(CollegeTrainingOrderModel::STATUS_MERCHANT_CONFIRM);
    }


    /**
     * 创业商学院 - 商家待确认
     * @param $status
     * @return array
     */
    public function getCollegeData($status): array
    {
        //获得商学院管理员
        $collegeManager = ConfigModel::getCollegeManager();  //todo 目前处理人就这个
        $list           = [];
        CollegeTrainingOrderModel::query()
            ->where('status', $status)
            ->get()
            ->each(function (CollegeTrainingOrderModel $collegeTrainingOrderModel) use (&$list, $collegeManager) {
                foreach ($collegeManager as $manager) {
                    $userId    = $manager;
                    $createdAt = $collegeTrainingOrderModel->createdAt->timestamp;
                    $uuid      = $collegeTrainingOrderModel->id;
                    $list[]    = $this->taskResponse($userId, $createdAt, $uuid);
                }
            })->toArray();
        return $list;
    }


    /**
     * 工单-功能开通-外卖  美团+饿了么
     * @return array
     */
    public function task181(): array
    {
        return $this->getWorkOrderList(OrderEnum::PROBLEM_PID_5, [OrderEnum::PROBLEM_PID_5_CHILDREN_101, OrderEnum::PROBLEM_PID_5_CHILDREN_102]);
    }


    /**
     * 工单-功能开通-私域
     * @return array
     */
    public function task182(): array
    {
        return $this->getWorkOrderList(OrderEnum::PROBLEM_PID_5, [OrderEnum::PROBLEM_PID_5_CHILDREN_100]);
    }


    /**
     * 工单-功能开通-点评
     * @return array
     */
    public function task183(): array
    {
        return $this->getWorkOrderList(OrderEnum::PROBLEM_PID_5, [OrderEnum::PROBLEM_PID_5_CHILDREN_103]);
    }


    /**
     * 工单-功能开通-抖音
     * @return array
     */
    public function task184(): array
    {
        return $this->getWorkOrderList(OrderEnum::PROBLEM_PID_5, [OrderEnum::PROBLEM_PID_5_CHILDREN_104]);
    }


    /**
     * 工单-仓储配送
     * @return array
     */
    public function task185(): array
    {
        return $this->getWorkOrderList(OrderEnum::PROBLEM_PID_1);
    }


    /**
     * 工单-品控异常
     * @return array
     */
    public function task187(): array
    {
        return $this->getWorkOrderList(OrderEnum::PROBLEM_PID_2);
    }


    /**
     * 工单-设备软装-设备
     * @return array
     */
    public function task186(): array
    {
        return $this->getWorkOrderList(OrderEnum::PROBLEM_PID_4, [OrderEnum::PROBLEM_PID_4_CHILDREN_1, OrderEnum::PROBLEM_PID_4_CHILDREN_2, OrderEnum::PROBLEM_PID_4_CHILDREN_13, OrderEnum::PROBLEM_PID_4_CHILDREN_8], [MissionEnum::STEP_4]);
    }


    /**
     * 工单-设备软装-软装
     * @return array
     */
    public function task188(): array
    {
        return $this->getWorkOrderList(OrderEnum::PROBLEM_PID_4, [OrderEnum::PROBLEM_PID_4_CHILDREN_5]);
    }


    /**
     * 工单-设备软装-吧台
     * @return array
     */
    public function task189(): array
    {
        return $this->getWorkOrderList(OrderEnum::PROBLEM_PID_4, [OrderEnum::PROBLEM_PID_4_CHILDREN_6]);
    }


    /**
     * 工单-设备软装-道具
     * @return array
     */
    public function task190(): array
    {
        return $this->getWorkOrderList(OrderEnum::PROBLEM_PID_4, [OrderEnum::PROBLEM_PID_4_CHILDREN_7]);
    }


    /**
     * 获得cruise处理人
     * @return array
     * @throws GuzzleException
     */
    public function getCruiseProcess($step, $stepStatus, $cruiseOrderModel)
    {
        $handleIds = []; //处理人
        if ($cruiseOrderModel->type == CruiseOrderModel::TYPE_NEW_SHOP_HELP) {
            switch ($step) {
                case CruiseOrderProcessModel::PROCESS_CREATED:
                    $nextStep = CruiseOrderProcessModel::PROCESS_SUBMITTED;
                    break;
                case CruiseOrderProcessModel::PROCESS_SUBMITTED:
                case CruiseOrderProcessModel::PROCESS_MODIFIED:
                    $nextStep = CruiseOrderProcessModel::PROCESS_REVIEWED;
                    break;
                case CruiseOrderProcessModel::PROCESS_REVIEWED:
                    if ($stepStatus == CruiseOrderProcessModel::STATUS_OFF) {
                        $nextStep = CruiseOrderProcessModel::PROCESS_MODIFIED;
                    } else {
                        $nextStep = CruiseOrderProcessModel::PROCESS_COMPLETED;
                    }
                    break;
                default:
                    $nextStep = CruiseOrderProcessModel::PROCESS_COMPLETED;
            }
            $handleIds = $cruiseOrderModel->getCurrentHandlerInfo($nextStep)['id'] ?? [];
        } elseif ($cruiseOrderModel->type == CruiseOrderModel::TYPE_CHECK) {
            switch ($step) {
                case CruiseOrderProcessModel::PROCESS_CREATED:
                    if ((int)optional($cruiseOrderModel->checkTaskDetailRel)->sysTemplate == CruiseTemplateModel::SYS_TEMPLATE_SPECIAL && (int)optional($cruiseOrderModel->checkTaskDetailRel)->checkType == CruiseCheckDetailModel::CHECK_TYPE_THIRD_PARTY) {
                        $nextStep = CruiseOrderProcessModel::PROCESS_RECEIVE;
                    } else {
                        $nextStep = CruiseOrderProcessModel::PROCESS_SUBMITTED;
                    }
                    break;
                case CruiseOrderProcessModel::PROCESS_RECEIVE:
                    $nextStep = CruiseOrderProcessModel::PROCESS_SUBMITTED;
                    break;
                case CruiseOrderProcessModel::PROCESS_SUBMITTED:
                case CruiseOrderProcessModel::PROCESS_MODIFIED:
                    if ((int)optional($cruiseOrderModel->checkTaskDetailRel)->sysTemplate == CruiseTemplateModel::SYS_TEMPLATE_SPECIAL && (int)optional($cruiseOrderModel->checkTaskDetailRel)->checkType == CruiseCheckDetailModel::CHECK_TYPE_THIRD_PARTY) {
                        $nextStep = CruiseOrderProcessModel::PROCESS_EXT_REVIEWED;
                    } else {
                        $nextStep = CruiseOrderProcessModel::PROCESS_REVIEWED;
                    }
                    break;
                case CruiseOrderProcessModel::PROCESS_REVIEWED:
                    if ((int)optional($cruiseOrderModel->checkTaskDetailRel)->sysTemplate == CruiseTemplateModel::SYS_TEMPLATE_SPECIAL && (int)optional($cruiseOrderModel->checkTaskDetailRel)->checkType == CruiseCheckDetailModel::CHECK_TYPE_THIRD_PARTY) {
                        $nextStep = CruiseOrderProcessModel::PROCESS_REVIEWED;
                    } else {
                        $nextStep = CruiseOrderProcessModel::PROCESS_MODIFIED;
                    }
                    break;
                case CruiseOrderProcessModel::PROCESS_EXT_REVIEWED:
                    if ($cruiseOrderModel->status == CruiseOrderModel::STATUS_NOT_MODIFY) {
                        $nextStep = CruiseOrderProcessModel::PROCESS_MODIFIED;
                    } else {
                        $nextStep = CruiseOrderProcessModel::PROCESS_REVIEWED;
                    }
                    break;
                default:
                    $nextStep = CruiseOrderProcessModel::PROCESS_COMPLETED;
            }
            if ((int)optional($cruiseOrderModel->checkTaskDetailRel)->sysTemplate == CruiseTemplateModel::SYS_TEMPLATE_SPECIAL && (int)optional($cruiseOrderModel->checkTaskDetailRel)->checkType == CruiseCheckDetailModel::CHECK_TYPE_THIRD_PARTY) {
                $handleIds = (new CheckInputService($cruiseOrderModel->id))->getCurrentHandlerInfo($nextStep)['userIds'] ?? [];
            } else {
                if ($nextStep == CruiseOrderModel::STATUS_NOT_REVIEW) {
                    $kuafuManager = ConfigModel::getCruiseCheckAudit($cruiseOrderModel->checkTaskDetailRel->companyId, $cruiseOrderModel->type ?? 0, FsCompanyRoleMappingModel::ROLE_ID_KF_REVIEWER);
                    $handleIds    = $kuafuManager;
                } else {
                    $handleIds = (new CheckInputService($cruiseOrderModel->id))->getCurrentHandlerInfo($nextStep)['userIds'] ?? [];
                }
            }
        }
        return (array)$handleIds;
    }


    /**
     * 获得工单数据
     * @param $problemPid
     * @param array $problemChildIds
     * @return array
     */
    public function getWorkOrderList($problemPid, array $problemChildIds = [], array $step = []): array
    {
        $list = [];
        WorkOrderApplyMainModel::query()
            ->where('problemPid', $problemPid)
            ->whereIn('step', [MissionEnum::STEP_1, MissionEnum::STEP_2, MissionEnum::STEP_3, MissionEnum::STEP_4, MissionEnum::STEP_5, MissionEnum::STEP_6])
            ->when(count($problemChildIds), function ($query) use ($problemChildIds) {
                $query->whereIn('problemChildId', $problemChildIds);
            })
            ->when(count($step), function ($query) use ($step) {
                $query->whereIn('step', $step);
            })
            ->with(['managerRel'])
            ->get()
            ->each(function (WorkOrderApplyMainModel $applyMainModel) use (&$list) {
                $userIds = [];
                foreach ($applyMainModel->managerRel as $managerRel) {
                    if (!mb_strlen($managerRel->remark) && $managerRel->role == WorkOrderUserRoleModel::ROLE_TYPE_1 && $managerRel->status == MissionEnum::MISSION_STATUS_0) {
                        $userIds[] = $managerRel->managerId;
                    }
                    if (!mb_strlen($managerRel->solution) && $managerRel->status == MissionEnum::MISSION_STATUS_0) {
                        $userIds[] = $managerRel->managerId;
                    }
                }
                foreach (array_unique($userIds) as $userId) {
                    $createdAt = $applyMainModel->createdAt->timestamp;
                    $uuid      = $applyMainModel->id;
                    $list[]    = $this->taskResponse($userId, $createdAt, $uuid);
                }
            });
        return $list;
    }

}
